%% Computing the posterior mean and variance for tau

function [bar_tau, bar_V_tau] = compute_posterior_moments(hat_theta, rep_base_se2, nu)
    % INPUT: baseline study data hat_theta, reported baseline study se^2 rep_base_se2, EB estimate nu
    % OUTPUT: posterior mean bar_tau, posterior variance bar_V_tau

    % Initialization
    N = length(hat_theta);  % Number of rows
    bar_tau = zeros(N, 1);  
    bar_V_tau = zeros(N, 1);   

    for i = 1:N
        % Extract non-NaN elements for the current row
        current_theta = hat_theta{i};
        current_se2 = rep_base_se2{i};
        
        % Update the posterior variance
        bar_V_tau(i) = sum(1 ./ (nu + current_se2))^-1;      

        % Update the posterior mean
        bar_tau(i) = bar_V_tau(i) * sum(current_theta ./ (nu + current_se2));         
    end
end
